package com.panshi.service.impl;

import com.panshi.constant.Constant;
import com.panshi.dao.ICommentDao;
import com.panshi.entity.AppUser;
import com.panshi.po.CommentPO;
import com.panshi.service.FeignAppUserClient;
import com.panshi.service.ICommentService;
import com.panshi.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ICommentServiceImpl implements ICommentService {

    @Resource
    private ICommentDao iComentDao;

    @Autowired
    private FeignAppUserClient feignAppUserClient;//查询用户信息的接口

    /**
     * 查询一级评论
     * @param zxId
     * @return
     */
    @Override
    public Map<String,Object> queryCommentByZxId(String zxId) {

        Map<String ,Object> map = new HashMap();

        try{
            List<CommentPO> list = iComentDao.queryCommentByZxId(zxId);

            String userId = null;
            for (CommentPO a: list) {
                userId = a.getUserId()
                ;
                if(userId!= null && userId != "" && userId.equals(a.getUserId())){
                    // 查询回答用户信息
                    AppUser appUser = feignAppUserClient.queryUserInfo(Integer.valueOf(a.getUserId()));
                    a.setAppUser(appUser);
                }

            }

            map.put("data",list);
            map.put("static",true);
            map.put("msg","查询成功！");
        }catch (Exception e){
            map.put("static",false);
            map.put("msg","查询失败！");
            e.printStackTrace();
        }

        return map;
    }

    /**
     * 查询回复
     * @param commentId
     * @return
     */
    @Override
    public Map<String,Object> queryReplyByCommentId(String commentId) {

        Map<String ,Object> map = new HashMap();

        try{
            List<CommentPO> list = iComentDao.queryReplyByCommentId(commentId);

            String userId = null;
            for (CommentPO a: list) {
                userId = a.getUserId();
                if(userId!= null && userId != "" && userId.equals(a.getUserId())){
                    // 查询回答用户信息
                    AppUser appUser = feignAppUserClient.queryUserInfo(Integer.valueOf(a.getUserId()));
                    a.setAppUser(appUser);
                }

                if(!a.getCommentId().equals(a.getCoverCommentId())){
                    // 查询回答用户信息
                    AppUser coverUser = feignAppUserClient.queryUserInfo(Integer.valueOf(a.getCoverUserId()));
                    a.setCoverUser(coverUser);
                }
            }

            map.put("data",list);
            map.put("static",true);
            map.put("msg","查询成功！");
        }catch (Exception e){
            map.put("static",false);
            map.put("msg","查询失败！");
            e.printStackTrace();
        }

        return map;

    }

    @Override
    public Map<String,Object> addComment(CommentVO commentVO) {
        Map<String ,Object> map = new HashMap();

        int type =  iComentDao.addComment(commentVO);

        if (Constant.ADDCOMMENT_STATUS_1 == type){
            map.put("msg","增加成功");
            map.put("status",true);
        }else {
            map.put("msg","增加失败");
            map.put("status",false);
        }

        return map;
    }
}
